class Solution(object):
    def longestValidParentheses(self, s):
        stack = []
        stack.append(-1)
        n = len(s)
        length = 0
        max_length = 0
        for i in range(n):
            if s[i] == "(":
                stack.append(i)
            else:
                stack.pop()
                if not stack:
                    stack.append(i)
                else:
                    length = i - stack[-1]
                    max_length = max(length, max_length)
        return max_length


data = Solution()
s = "()((())"
print(data.longestValidParentheses(s))
